const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const data = require('../data');
const replySQL = require('../../db/replySQL');
const userSQL = require('../../db/userSQL')
const async = require('async')
let partitionList = data.partitionList;

router.post('/getreply', bodyParser.json(), (req, res) => {
    // 检查主帖ID是否为空
    if (req.body["postID"] == undefined
        || req.body["postID"] == null
        || typeof req.body["postID"] != "number") {
        res.json({
            err: 1,
            message: '主帖ID为空'
        })
    } else if (req.body["isAnnouncement"] == undefined
        || req.body["isAnnouncement"] == null
        || typeof req.body["isAnnouncement"] != "number") {
        res.json({
            err: 1,
            message: '帖子类别为空'
        })
    } else {
        var postID = req.body["postID"]
        var isAnnouncement = req.body["isAnnouncement"]
        replySQL.getreply(postID, isAnnouncement, (post, replies, partition) => {
            var tempReplyList = []
            var tempResult = {
                err: 200,
                message: '获取回帖成功',
                partitionID: partition.partitionID,
                partitionName: partition.partitionName,
                title: isAnnouncement == 0 ? post.postTitle : post.announcementTitle,
                content: isAnnouncement == 0 ? post.postContent : post.announcementContent,
                postTime: isAnnouncement == 0 ? post.postTime : post.announcementTime,
                lastTime: isAnnouncement == 0 ? post.postReplyTime : post.announcementReplyTime,
                userID: isAnnouncement == 0 ? post.postUserID : post.announcementUserID,
                userName: '',
                replyNum: isAnnouncement == 0 ? post.postRepCount : 0,
                replyList: []
            }
            var tasks = []
            function getNameAndContent1 (done) {
                userSQL.getinfo(tempResult.userID, (result) => {
                    tempResult.userName = result[0].userName
                    replySQL.readFile(tempResult.content, (filecontent) => {
                        tempResult.content = filecontent
                        done(null, null)
                    })
                })
            }
            tasks.push(getNameAndContent1)
            replies.forEach(reply => {
                var tempReply = {
                    replyID: reply.replyID,
                    userID: reply.replyUserID,
                    userName: '',
                    content: '',
                    replyFloor: reply.replyFloor,
                    replyTime: reply.replyTime
                }
                function getNameAndContent (done) {
                    userSQL.getinfo(reply.replyUserID, (result) => {
                        tempReply.userName = result[0].userName
                        replySQL.readFile(reply.replyContent, (filecontent) => {
                            tempReply.content = filecontent
                            tempReplyList.push(tempReply)
                            done(null, null)
                        })
                    })
                }
                tasks.push(getNameAndContent)
            });
            async.series(tasks, (err, results) => {
                tempResult.replyList = tempReplyList
                res.json(tempResult)
            })
        })
    }
})

module.exports = router;